<?php
namespace Home\Controller;

use Think\Controller;

class UserController extends Controller
{

    public function login()
    {
        if (IS_POST) {
            $code = I('post.code');
            $yzm  = $this->check_verify($code);
            // if(!$yzm){
            //     $this->error('验证码输入错误');
            // }
            $name = I('post.name');
            $pwd  = I('post.pwd');
            $user = M('suser');
            $res  = $user->where(array('name' => $name, 'pwd' => md5($pwd), 'active' => '1'))->select()[0];
            if ($res) {
                session('name', $res['name']);
                session('id', $res['id']);
                session('islogin',1);
                $arr = array('log_time' => time(), 'status' => '1');
                M('suser')->where(array('id' => $res['id']))->save($arr);
                $this->success('登录成功，即将前往商城主页', U('Index/index'));
            } else {
                $this->error('用户名或密码输入错误！');
            }
        } else {
            $this->display();
        }
    }

    public function register()
    {
        if (IS_POST) {
            $suser  = D('suser');
            $create = $suser->create(I('post.'));
            if ($create) {
                if ($id = $suser->add()) {
                    import('Behavior.Mail');
                    $email   = $create['email'];
                    $title   = '账号激活邮件';
                    $content = 'shop.com' . U('active?id=' . $id);
                    $res1    = SendMail($email, $title, $content);
                    $this->success('账号注册成功!请及时激活您的账号', U('login'));
                } else {
                    $this->error($suser->getError());
                }
            } else {
                $this->error($suser->getError());
            }
        } else {
            $this->display();
        }
    }

    public function password_edit()
    {
        if (IS_POST) {
            $arr   = array('pwd' => md5(I('post.pwd')));
            $suser = D('suser');
            $res   = $suser->where(array('name' => session('name'), 'pwd' => md5(I('post.opwd'))))->select();
            if ($res) {
                $res2 = $suser->where(array('name' => session('name')))->save($arr);
                if ($res2) {
                    $this->success('密码修改成功，请重新登录', U('login'));
                } else {
                    $this->error($suser->getError());
                }
            } else {
                $this->error('原密码输入错误');
            }
        } else {
            $flow       = D('flow')->where(array('uid' => session('id')))->select();
            $flow_count = count($flow);
            $this->assign('flow_count', $flow_count);
            $this->display();
        }
    }

    public function logout(){
        session(null);
        $this->success('已退出登录',U('user/login'));
    }

    public function member()
    {
        $res = M('order')->join('onethink_property on onethink_order.pid = onethink_property.id')->where(array('onethink_order.uid' => session('id')))->select();
        foreach ($res as &$v) {
            $v['goods'] = M('goods')->where(array('id' => $v['gid']))->select()[0]['title'];
            $v['total'] = $v['cost'] * $v['num'];
        }
        $flow       = D('flow')->where(array('uid' => session('id')))->select();
        $flow_count = count($flow);
        $this->assign('flow_count', $flow_count);
        $this->assign('res', $res);
        $this->display();
    }

    public function member_info()
    {
        $suser = M('suser');
        if (IS_POST) {
            $create = $suser->create(I('post.'));
            if ($create) {
                $res = $suser->where(array('id' => session('id')))->save();
                if ($res) {
                    $this->success('编辑成功');
                } else {
                    $this->error($suser->getError());
                }
            } else {
                $this->error($suser->getError());
            }
        } else {
            $res        = $suser->where(array('id' => session('id')))->select()[0];
            $flow       = D('flow')->where(array('uid' => session('id')))->select();
            $flow_count = count($flow);
            $this->assign('flow_count', $flow_count);
            $this->assign('res', $res);
            $this->display();
        }

    }

    public function estimate()
    {
        $res   = M('estimate')->field('onethink_estimate.*,onethink_property.gid,onethink_property.color,onethink_property.size,onethink_property.cost')->join('onethink_property on onethink_estimate.pid = onethink_property.id')->where(array('uid' => session('id')))->select();
        $suser = M('suser');
        foreach ($res as &$v) {
            $v['pic'] = $suser->where(array('id' => $v['uid']))->select()[0]['pic'];
            if ($v['pic'] == '') {
                $v['pic'] = '2017-07-04/man.jpg';
            }
            $v['title'] = M('goods')->where(array('id' => $v['gid']))->select()[0]['title'];
            $v['name']  = $suser->where(array('id' => $v['uid']))->select()[0]['name'];
            $v['image'] = explode(',', $v['image'], 6);
        }
        $flow       = D('flow')->where(array('uid' => session('id')))->select();
        $flow_count = count($flow);
        $this->assign('flow_count', $flow_count);
        $this->assign('res', $res);
        $this->display();
    }

    public function estimate_delete()
    {
        $res = M('estimate')->where(array('id' => I('get.id')))->delete();
        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function estimate_update()
    {
        if (IS_POST) {
            $estimate1    = M('estimate');
            $pic          = $this->upload();
            $_POST['pic'] = $pic;
            $res          = $estimate1->create(I('post.'));
            if ($res) {
                if ($estimate1->where(array('id' => I('get.id')))->save()) {
                    $this->success('修改成功', U('estimate'));
                } else {
                    $this->error($estimate1->getError());
                }
            } else {
                $this->error($estimate1->getError());
            }
        } else {
            $estimate = M('estimate')->field('onethink_estimate.*,onethink_property.gid,onethink_property.color,onethink_property.size,onethink_property.cost')->join('onethink_property on onethink_estimate.pid = onethink_property.id')->where(array('onethink_estimate.id' => I('get.id')))->select();
            foreach ($estimate as &$v) {
                $v['title'] = M('goods')->where(array('id' => $v['gid']))->select()[0]['title'];
            }
            $this->assign('estimate', $estimate[0]);
            $this->display();
        }
    }

    public function address()
    {
        if (IS_POST) {
            dump(I('post.'));
            $_POST['uid'] = session('id');
            $address      = D('address');
            $res          = $address->create(I('post.'));
            if ($res) {
                if ($address->add()) {
                    $this->success('添加成功');
                } else {
                    $this->error($address->getError());
                }
            } else {
                $this->error($address->getError());
            }
        } else {
            $fid       = I('get.fid');
            $res       = M('address')->where(array('uid' => session('id')))->select();
            $placename = M('placename')->where(array('fid' => 0))->select();

            foreach ($res as &$v) {
                $v['address1'] = M('placename')->where(array('id' => $v['address1']))->select()[0]['name'];
                $v['address2'] = M('placename')->where(array('id' => $v['address2']))->select()[0]['name'];
                $v['address3'] = M('placename')->where(array('id' => $v['address3']))->select()[0]['name'];
            }
            $flow       = D('flow')->where(array('uid' => session('id')))->select();
            $flow_count = count($flow);
            $this->assign('flow_count', $flow_count);
            $this->assign('placename', $placename);
            $this->assign('res', $res);
            $this->display();
        }
    }

    public function address_update()
    {
        $address = M('address');
        if (IS_POST) {
            $res = $address->create(I('post.'));
            if ($res) {
                if ($address->where(array('id' => session('get_id')))->save()) {
                    session('get_id', null);
                    $this->success('修改成功', U('address'));
                } else {
                    $this->error($address->getError());
                }
            } else {
                $this->error($address->getError());
            }
        } else {
            session('get_id', I('get.id'));
            $address1  = $address->where(array('id' => I('get.id')))->select()[0];
            $placename = M('placename')->where(array('fid' => 0))->select();
            $this->assign('placename', $placename);
            $this->assign('address', $address1);
            $this->display();
        }
    }

    public function address_delete()
    {
        $res = M('address')->where(array('id' => I('get.id')))->delete();
        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error(M('address')->getError());
        }
    }

    public function placename()
    {
        $fid        = I('get.fid');
        $placename2 = M('placename')->where(array('fid' => $fid))->select();
        if ($placename2) {
            $this->assign('placename2', $placename2);
            $html             = $this->fetch('placename2Ajax');
            $return['status'] = 1;
            $return['info']   = 'success';
            $return['data']   = $html;
        } else {
            $return['status'] = 0;
            $return['info']   = 'error';
            $return['data']   = null;
        }
        $this->ajaxReturn($return);
    }

    public function placename3()
    {
        $fid        = I('get.fid');
        $placename3 = M('placename')->where(array('fid' => $fid))->select();
        if ($placename3) {
            $this->assign('placename3', $placename3);
            $html             = $this->fetch('placename3Ajax');
            $return['status'] = 1;
            $return['info']   = 'success';
            $return['data']   = $html;
        } else {
            $return['status'] = 0;
            $return['info']   = 'error';
            $return['data']   = null;
        }
        $this->ajaxReturn($return);
    }

    public function password_forget()
    {
        if (IS_POST) {
            $suser = M('suser');
            $error = $suser->where(array('name' => I('post.name'), 'email' => I('post.email'), 'pwd' => md5(I('post.opwd'))))->select();
            $arr   = array('pwd' => md5(I('post.pwd')));
            if (!$error) {
                $this->error('邮箱不正确或密码输入不正确');
            } else {
                $res = $suser->where(array('name' => I('post.name')))->save($arr);
                if ($res) {
                    import('Behavior.Mail');
                    $email   = I('post.email');
                    $title   = '账号变动提醒';
                    $content = I('post.name') . '用户你好！你位于shop CZ商城的账号密码已被修改，如果不是本人操作，请及时与工作人员联系 ';
                    $res1    = SendMail($email, $title, $content);
                    $this->success('密码修改成功，即将返回登录页面', U('User/login'));
                } else {
                    $this->error($suser->getError());
                }
            }
        } else {
            $this->display();
        }
    }

    public function email_hint()
    {
        $email   = I('get.email');
        $pattern = '/\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/';
        $style   = 'color:green;';
        $res     = preg_match($pattern, $email);
        if (!$res) {
            echo '邮箱格式不正确';
        } else {
            echo '<span style="' . $style . '">邮箱格式可用</span>';
        }
    }

    public function pwd_hint()
    {
        $pwd    = I('get.pwd');
        $style  = 'color:green;';
        $length = strlen($pwd);
        if ($length < 6 || $length > 16) {
            echo '密码长度为6到16位';
        } else {
            echo '<span style="' . $style . '">密码长度可用</span>';
        }
    }

    public function repwd_hint()
    {
        $pwd   = I('get.pwd');
        $repwd = I('get.repwd');
        if ($pwd != $repwd) {
            echo '两次输入的密码不相同';
        }
    }

    public function yzm()
    {
        $config = array(
            'fontSize' => 20, // 验证码字体大小
            'length'   => 4, // 验证码位数
            'useNoise' => false, // 关闭验证码杂点
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    public function active()
    {
        $id   = I('get.id');
        $user = D('suser');
        $res  = $user->where(array('id' => $id))->setField('active', 1);
        if ($res) {
            $this->success('账号激活成功,即将返回登录', U('login'));
        }
    }

    public function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    public function upload()
    {
        $upload           = new \Think\Upload(); // 实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = '/Uploads/'; // 设置附件上传目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {
            // 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
            // 上传成功
            $path = '';
            foreach ($info as $v) {
                $path .= $v['savepath'] . $v['savename'] . ',';
            }
            return rtrim($path, ',');
        }
    }
}
